package com.danco.sn.model.dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import com.danco.sn.apientities.INews;
import com.danco.sn.apientities.IUser;
import com.danco.sn.entities.NewsCommunity;
import com.danco.sn.entities.NewsUser;


public class NewsDAO extends GenericDAO<INews> {

	private static final String queryFriendNews = "Select * from news_user  inner join (select * from friends_table where id_friend=?)s using(id_user) order by news_date desc";
	private static final String queryCommunityNews = "Select * from news_community join (select id_community from community_users where id_user=? union select id_community from community where id_moderator=?)s  using(id_community) order by news_date desc";

	public NewsDAO() {
		super(INews.class);
	}

	@SuppressWarnings("unchecked")
	public List<INews> getListNewsFriend(Session session, IUser user)
			throws HibernateException {
		SQLQuery sqlQuery = session.createSQLQuery(queryFriendNews).addEntity(
				NewsUser.class);
		sqlQuery.setString(0, user.getIdUser().toString());
		sqlQuery.setMaxResults(15);
		List<INews> listNews = sqlQuery.list();
		return listNews;
	}

	@SuppressWarnings("unchecked")
	public List<INews> getListNewsCommunity(Session session, IUser user)
			throws HibernateException {
		SQLQuery sqlQuery = session.createSQLQuery(queryCommunityNews)
				.addEntity(NewsCommunity.class);
		sqlQuery.setString(0, user.getIdUser().toString());
		sqlQuery.setString(1, user.getIdUser().toString());
		sqlQuery.setMaxResults(15);
		List<INews> listNews = sqlQuery.list();
		return listNews;
	}

}
